package cn.ubeta.b2c.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import cn.ubeta.b2c.R;
import cn.ubeta.b2c.common.AnalyseActivity;
import cn.ubeta.b2c.common.LoginCommon;
import cn.ubeta.b2c.utils.AppConfig;
import cn.ubeta.b2c.utils.Constants;
import cn.ubeta.b2c.utils.TitleBarHelper;
import cn.ubeta.b2c.utils.ToastHelper;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Created by denny on 2016/10/25.
 */

public class FillBillActivity extends AnalyseActivity {

    private RadioButton mRbCompany;
    private View mLayoutCompany;

    private EditText mEtComanyName;
    private EditText mEtMobile;

    private RadioButton mRbPage;

    @Override
    public String getPageName() {
        return "填写发票页";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        onCreate(savedInstanceState, R.layout.activity_fill_bill);

        TitleBarHelper.setRightAction(this, "保存", v -> saveBill());

        mLayoutCompany = findViewById(R.id.ll_bill_company);
        mRbCompany = ((RadioButton)findViewById(R.id.rb_bill_company));
        mRbCompany.setOnCheckedChangeListener((compoundButton, checked) -> mLayoutCompany.setVisibility(checked ? VISIBLE : GONE));

        mEtComanyName = (EditText) findViewById(R.id.tv_bill_comp_name);
        mEtMobile = (EditText) findViewById(R.id.tv_bill_mobile);

        mRbPage = ((RadioButton)findViewById(R.id.rb_bill_paper));

        // 初始化
        String title = AppConfig.getPreConfigBillTitle();
        String mobile = AppConfig.getPreConfigBillMobile();
        if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(mobile)) {
            mRbCompany.setChecked(true);
            mEtComanyName.setText(title);
            mEtMobile.setText(mobile);

            mLayoutCompany.setVisibility(VISIBLE);
        }
        if (!AppConfig.getPreConfigBillIsElec()) {
            mRbPage.setChecked(true);
        }
    }

    /**
     * 保存发票
     */
    private void saveBill() {
        String title = "";
        String mobile = "";
        if (mRbCompany.isChecked()) {
            title = mEtComanyName.getEditableText().toString();
            mobile = mEtMobile.getEditableText().toString();
            if (TextUtils.isEmpty(title)) {
                ToastHelper.show("请填写公司名称");
                return;
            } else if (TextUtils.isEmpty(mobile)) {
                ToastHelper.show("请填写手机号");
                return;
            } else if (!checkPhone(mobile)) {
                ToastHelper.show("手机号格式错误");
                return;
            }
        }
        boolean isElec = !mRbPage.isChecked();
        AppConfig.savePreConfigBillInfo(title, isElec, mobile);
        setResult(RESULT_OK);
        finish();
    }

    /**
     * 检查手机号
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone) {
        return phone.matches("^1[0-9]{10}");
    }

    /**
     * 跳转地址列表
     * @param activity
     */
    public static void gotoFillBill(Activity activity) {
        if (LoginCommon.isLogin()) {
            Intent intent = new Intent(activity, FillBillActivity.class);
            activity.startActivityForResult(intent, Constants.REQ_CODE_FILL_BILL);
        } else {
            LoginCommon.gotoLogin(activity);
        }
    }
}
